package com.cake.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cake.dao.XxCouponMapper;
import com.cake.dao.XxCouponMemberRankMapper;
import com.cake.dao.XxCouponProductCategoryMapper;
import com.cake.entity.XxCoupon;
import com.cake.entity.XxCouponMemberRankKey;
import com.cake.entity.XxCouponProductCategoryKey;
import com.cake.service.base.BaseService;
import com.cake.service.inter.IXxCouponService;

/**
 * 优惠券业务层
 */
@Service
@Transactional
public class XxCouponServiceImpl extends BaseService<XxCoupon> implements IXxCouponService{

	@Autowired
	private XxCouponMapper xxCouponMapper;
	
	@Autowired
	private XxCouponProductCategoryMapper xxCouponProductCategoryMapper;
	
	@Autowired
	private XxCouponMemberRankMapper xxCouponMemberRankMapper;
	
	@Override
	public Integer insertOrUpdateCoupon(XxCoupon xxCoupon) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		xxCoupon.setBeginDate(format.parse(xxCoupon.getBeginDateStr() + " 00:00:00"));
		xxCoupon.setEndDate(format.parse(xxCoupon.getEndDateStr() + " 00:00:00"));
		xxCoupon.setIsEnabled(xxCoupon.getIsEnabledFlag() == 1);
		Integer saveCouponCode = 0;
		if(xxCoupon.getId() == null){
			xxCoupon.setCreateDate(new Date());
			saveCouponCode = xxCouponMapper.insert(xxCoupon);
		}else{
			saveCouponCode = xxCouponMapper.updateByPrimaryKeySelective(xxCoupon);
		}
		if(saveCouponCode > 0){
			xxCouponMemberRankMapper.deleteByCoupon(xxCoupon.getId());
			xxCouponProductCategoryMapper.deleteByCoupon(xxCoupon.getId());
			
			String productCategoryIds = xxCoupon.getProductCategoryIds();
			String memberRankIds = xxCoupon.getMemberRankIds();
			if(productCategoryIds.length() > 0){
				insertCouponAndProductCategory(productCategoryIds.split(","), xxCoupon.getId());
			}
			if(memberRankIds.length() > 0){
				insertCouponAndMemberRank(memberRankIds.split(","), xxCoupon.getId());
			}
		}
		return saveCouponCode;
	}

	/**
	 * 优惠券与商品类型中间表添加
	 * @param productCategoryIds
	 * @param couponId
	 */
	private void insertCouponAndProductCategory(String[] productCategoryIds, Long couponId){
		for (int i = 0; i < productCategoryIds.length; i++) {
			XxCouponProductCategoryKey xxCouponProductCategoryKey = new XxCouponProductCategoryKey();
			xxCouponProductCategoryKey.setCoupons(couponId);
			xxCouponProductCategoryKey.setProductCategories(Long.parseLong(productCategoryIds[i]));
			if(xxCouponProductCategoryMapper.selectCouponProductCategoryKey(xxCouponProductCategoryKey) == null){
				xxCouponProductCategoryMapper.insert(xxCouponProductCategoryKey);
			}
		}
	}
	
	/**
	 * 优惠券与会员等级中间表添加
	 * @param memberRankIds
	 * @param couponId
	 */
	private void insertCouponAndMemberRank(String[] memberRankIds, Long couponId){
		for (int i = 0; i < memberRankIds.length; i++) {
			XxCouponMemberRankKey xxCouponMemberRankKey = new XxCouponMemberRankKey();
			xxCouponMemberRankKey.setCoupons(couponId);
			xxCouponMemberRankKey.setMemberRanks(Long.parseLong(memberRankIds[i]));
			if(xxCouponMemberRankMapper.selectCouponAndMemberRank(xxCouponMemberRankKey) == null){
				xxCouponMemberRankMapper.insert(xxCouponMemberRankKey);
			}
		}
	}

	@Override
	public XxCoupon selectCouponDetails(Long couponId) {
		XxCoupon xxCoupon = xxCouponMapper.selectByPrimaryKey(couponId);
		if(xxCoupon != null){
			 XxCouponMemberRankKey xxCouponMemberRankKey =  new XxCouponMemberRankKey();
			 xxCouponMemberRankKey.setCoupons(xxCoupon.getId());
			 XxCouponProductCategoryKey xxCouponProductCategoryKey = new XxCouponProductCategoryKey();
			 xxCouponProductCategoryKey.setCoupons(xxCoupon.getId());
			List<XxCouponMemberRankKey> couponMemberRanks = xxCouponMemberRankMapper.
					selectByCouponIdOrMemberRankId(xxCouponMemberRankKey);
			String memberRankIds = "";
			List<XxCouponProductCategoryKey> couponProductCategorys = xxCouponProductCategoryMapper.
					selectByCouponIdOrProductCategoryId(xxCouponProductCategoryKey);
			String productCategoryIds = "";
			for (int i = 0; i < couponMemberRanks.size(); i++) {
				if( i == couponMemberRanks.size() - 1){
					memberRankIds = memberRankIds + couponMemberRanks.get(i).getMemberRanks().toString();
				}else{
					memberRankIds = memberRankIds + couponMemberRanks.get(i).getMemberRanks().toString() + ",";
				}
			}
			for (int i = 0; i < couponProductCategorys.size(); i++) {
				if( i == couponProductCategorys.size() - 1){
					productCategoryIds = productCategoryIds + couponProductCategorys.get(i).getProductCategories().toString();
				}else{
					productCategoryIds = productCategoryIds + couponProductCategorys.get(i).getProductCategories().toString() + ",";
				}
			}
			xxCoupon.setMemberRankIds(memberRankIds);
			xxCoupon.setProductCategoryIds(productCategoryIds);
		}
		return xxCoupon;
	}

	@Override
	public Integer selectCouponByProductCategory(Long productCategoryId) {
		return xxCouponMapper.selectCouponByProductCategory(productCategoryId);
	}
}
